var cwsf = {
    setBtnStatus: function () {
        var row = $('#grid_cwsf').datagrid('getSelected');
        if (row && row.id) {
            //未导入
            if (row.czzt == 'wdr') {
                $("#btn_cwsf_import").show();//导入
                $("#btn_cwsf_cx").hide();//撤销
                $("#btn_cwsf_cz").hide();//重置
            }
            if (row.czzt == 'drcg') {
                $("#btn_cwsf_import").hide();//导入
                $("#btn_cwsf_cx").show();//撤销
                $("#btn_cwsf_cz").hide();//重置
            }
            if (row.czzt == 'drsb') {
                $("#btn_cwsf_import").hide();//导入
                $("#btn_cwsf_cx").hide();//撤销
                $("#btn_cwsf_cz").show();//重置
            }
            if (row.czzt == 'sccg') {
                $("#btn_cwsf_import").show();//导入
                $("#btn_cwsf_cx").hide();//撤销
                $("#btn_cwsf_cz").hide();//重置
            }
            if (row.czzt == 'scsb') {
                $("#btn_cwsf_import").hide();//导入
                $("#btn_cwsf_cx").hide();//撤销
                $("#btn_cwsf_cz").show();//重置
            }
        } else {
            $("#btn_cwsf_import").hide();//导入
            $("#btn_cwsf_cx").hide();//撤销
            $("#btn_cwsf_cz").hide();//重置
        }
    }
}


$(function () {
    //加载企业机构
    $("#cwsf_gldw").qyjgzcombobox();
    //表格初始加载
    if( $("[name='cwsf_type']:checked").val()==1|| !$("[name='cwsf_type']:checked").val()){
        $("#kmgystype").text("供应商：");
        $('#grid_cwsf').datagrid({
            url: $.webPath() + "/cwgl/cwsf/cwsfList",
            toolbar: "#toolbar_cwsf",
            fitColumns: true,
            singleSelect: true,
            pagination: true,
            pageNumber: 1,
            fit: true,
            rownumbers: true,
            striped: true,
            columns: [
                [
                    {field: 'bm', title: '单据号', width: 25,align: 'center'},
                    {field: 'sfkrq', title: '付款日期', width: 25,align: 'center'},
                    {field: 'sfklx', title: '付款类型', width: 15,align: 'center',
                        formatter: function (value) {
                            if (value == '1')
                                return '供应付款';
                            else
                                return '销售退款';
                        }
                    },
                    {field: 'gldw_qc', title: '关联企业', width: 30,align: 'center'},
                    {field: 'sfje', title: '付款金额（元）', width: 20,align: 'right',
                        formatter:function(value){
                            var sl = formatJe2(value,",");
                            return sl;
                        }  ,
                        styler:function(){
                            return 'color:green;font-size:14px;';
                        }
                    },
                    {field: 'pjlx_z', title: '票据类型', width: 15,align: 'center'},
                    {field: 'nbkhh_qc', title: '入账银行', width: 15,align: 'center'},
                    {field: 'kxlx', title: '款项类型', width: 15, align: 'center',
                        formatter: function (value) {
                            if (value == '1')
                                return '预付款';
                            if (value == '2')
                                return '应付款';
                            if (value == '3')
                                return '预收款';
                            if (value == '4')
                                return '应收款';
                        }
                    },
                    {field: 'shr_xm', title: '审核人', width: 12,align: 'center'},
                    {field: 'sbgs_jc', title: '公司名称', width: 20,align: 'center'},
                    {field:'voucherNum', title:'凭证号', width:15,align: 'center'},
                    {field: 'czzt', title: '状态', width: 10,align: 'center',formatter: function (value, row, index) {
                        if (value == 'wdr')
                            return "未导入";
                        if (value == 'drcg')
                            return "导入成功";
                        if (value == 'drsb')
                            return "导入失败";
                        if (value == 'sccg')
                            return "删除成功";
                        if (value == 'scsb')
                            return "删除失败";
                        return "";
                    }}
                ]
            ],
            onLoadSuccess:function(){
                var rows = $('#grid_cwsf').datagrid("getRows");
                if(rows.length>0){
                    $('#grid_cwsf').datagrid("selectRow", 0);
                }
                cwsf.setBtnStatus();
                $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
            },
            onClickRow:function(){
                cwsf.setBtnStatus();
            }
        });
    } else{
        $("#kmgystype").text("客户：");
        $('#grid_cwsf').datagrid({
            url: $.webPath() + "/cwgl/cwsf/cwsfList",
            toolbar: "#toolbar_cwsf",
            fitColumns: true,
            singleSelect: true,
            pagination: true,
            pageNumber:1,
            fit: true,
            rownumbers: true,
            striped: true,
            columns: [
                [
                    {field: 'bm', title: '单据号', width: 25,align: 'center'},
                    {field: 'sfkrq', title: '收款日期', width: 25,align: 'center'},
                    {field: 'sfklx', title: '收款类型', width: 15,align: 'center',
                        formatter: function (value) {
                            if (value == '3')
                                return '销售收款';
                            else
                                return '供应退款';
                        }
                    },
                    {field: 'gldw_qc', title: '关联单位', width: 30,align: 'center'},
                    {field: 'sfje', title: '付款金额（元）', width: 20,align: 'right',
                        formatter:function(value){
                            var sl = formatJe2(value,",");
                            return sl;
                        }  ,
                        styler:function(){
                            return 'color:green;font-size:14px;';
                        }
                    },
                    {field: 'kxlx', title: '款项类型', width: 15,align: 'center',
                        formatter: function (value) {
                            if (value == '1')
                                return '预付款';
                            if (value == '2')
                                return '应付款';
                            if (value == '3')
                                return '预收款';
                            if (value == '4')
                                return '应收款';
                        }
                    },
                    {field: 'pjlx_z', title: '票据类型', width: 15,align: 'center'},
                    {field: 'nbkhh_qc', title: '入账银行', width: 15,align: 'center'},
                    {field: 'shr_xm', title: '审核人', width: 12,align: 'center'},
                    {field: 'sbgs_jc', title: '公司名称', width: 20,align: 'center'},
                    {field:'voucherNum', title:'凭证号', width:15,align: 'center'},
                    {field: 'czzt', title: '状态', width: 10,align: 'center', formatter: function (value, row, index) {
                        if (value == 'wdr')
                            return "未导入";
                        if (value == 'drcg')
                            return "导入成功";
                        if (value == 'drsb')
                            return "导入失败";
                        if (value == 'sccg')
                            return "删除成功";
                        if (value == 'scsb')
                            return "删除失败";
                        return "";
                    }}
                ]
            ],
            onLoadSuccess:function(){
                var rows = $('#grid_cwsf').datagrid("getRows");
                if(rows.length>0){
                    $('#grid_cwsf').datagrid("selectRow", 0);
                }
                cwsf.setBtnStatus();
                $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
            },
            onClickRow:function(){
                cwsf.setBtnStatus();
            }
        });
    }

    //当类型切换时切换页面
    $("input[name='cwsf_type']").change(function () {
        if( $("[name='cwsf_type']:checked").val()==1 || !$("[name='cwsf_type']:checked").val()){
            $("#kmgystype").text("供应商：");
            $('#grid_cwsf').datagrid({
                url: $.webPath() + "/cwgl/cwsf/cwsfList?skflx=1",
                toolbar: "#toolbar_cwsf",
                fitColumns: true,
                singleSelect: true,
                pagination: true,
                pageNumber:1,
                fit: true,
                rownumbers: true,
                striped: true,
                columns: [
                    [
                        {field: 'bm', title: '单据号', width: 25,align: 'center'},
                        {field: 'sfkrq', title: '付款日期', width: 25,align: 'center'},
                        {field: 'sfklx', title: '付款类型', width: 15,align: 'center',
                            formatter: function (value) {
                                if (value == '1')
                                    return '供应付款';
                                else
                                    return '销售退款';
                            }
                        },
                        {field: 'gldw_qc', title: '关联企业', width: 30,align: 'center'},
                        {field: 'sfje', title: '付款金额（元）', width: 20,align: 'right',
                            formatter:function(value){
                                var sl = formatJe2(value,",");
                                return sl;
                            }  ,
                            styler:function(){
                                return 'color:green;font-size:14px;';
                            }
                        },
                        {field: 'pjlx_z', title: '票据类型', width: 15,align: 'center'},
                        {field: 'nbkhh_qc', title: '入账银行', width: 15,align: 'center'},
                        {field: 'kxlx', title: '款项类型', width: 15,align: 'center',
                            formatter: function (value) {
                                if (value == '1')
                                    return '预付款';
                                if (value == '2')
                                    return '应付款';
                                if (value == '3')
                                    return '预收款';
                                if (value == '4')
                                    return '应收款';
                            }
                        },
                        {field: 'shr_xm', title: '审核人', width: 12,align: 'center'},
                        {field: 'sbgs_jc', title: '公司名称', width: 20,align: 'center'},
                        {field:'voucherNum', title:'凭证号', width:15,align: 'center'},
                        {field: 'czzt', title: '状态',align: 'center', width: 10, formatter: function (value, row, index) {
                            if (value == 'wdr')
                                return "未导入";
                            if (value == 'drcg')
                                return "导入成功";
                            if (value == 'drsb')
                                return "导入失败";
                            if (value == 'sccg')
                                return "删除成功";
                            if (value == 'scsb')
                                return "删除失败";
                            return "";
                        }}
                    ]
                ],
                onLoadSuccess:function(){
                    var rows = $('#grid_cwsf').datagrid("getRows");
                    if(rows.length>0){
                        $('#grid_cwsf').datagrid("selectRow", 0);
                    }
                    cwsf.setBtnStatus();
                    $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
                },
                onClickRow:function(){
                    cwsf.setBtnStatus();
                }
            });
        }  else if( $("[name='cwsf_type']:checked").val()==3 || !$("[name='cwsf_type']:checked").val()){
            $("#kmgystype").text("客户：");
            $('#grid_cwsf').datagrid({
                url: $.webPath() + "/cwgl/cwsf/cwsfList?sfklx=3",
                toolbar: "#toolbar_cwsf",
                fitColumns: true,
                singleSelect: true,
                pagination: true,
                fit: true,
                pageNumber:1,
                rownumbers: true,
                striped: true,
                columns: [
                    [
                        {field: 'bm', title: '单据号', width: 25,align: 'center'},
                        {field: 'sfkrq', title: '收款日期', width: 25,align: 'center'},
                        {field: 'sfklx', title: '收款类型', width: 15,align: 'center',
                            formatter: function (value) {
                                if (value == '3')
                                    return '销售收款';
                                else
                                    return '供应退款';
                            }
                        },
                        {field: 'gldw_qc', title: '关联单位', width: 30,align: 'center'},
                        {field: 'sfje', title: '付款金额（元）', width: 20, align: 'right',
                            formatter:function(value){
                                var sl = formatJe2(value,",");
                                return sl;
                            }  ,
                            styler:function(){
                                return 'color:green;font-size:14px;';
                            }
                        },
                        {field: 'kxlx', title: '款项类型', width: 15,align: 'center',
                            formatter: function (value) {
                                if (value == '1')
                                    return '预付款';
                                if (value == '2')
                                    return '应付款';
                                if (value == '3')
                                    return '预收款';
                                if (value == '4')
                                    return '应收款';
                            }
                        },
                        {field: 'pjlx_z', title: '票据类型', width: 15,align: 'center'},
                        {field: 'nbkhh_qc', title: '入账银行', width: 15,align: 'center'},
                        {field: 'shr_xm', title: '审核人', width: 12,align: 'center'},
                        {field: 'sbgs_jc', title: '公司名称', width: 20,align: 'center'},
                        {field:'voucherNum', title:'凭证号', width:15,align: 'center'},
                        {field: 'czzt', title: '状态', align: 'center',width: 10, formatter: function (value, row, index) {
                            if (value == 'wdr')
                                return "未导入";
                            if (value == 'drcg')
                                return "导入成功";
                            if (value == 'drsb')
                                return "导入失败";
                            if (value == 'sccg')
                                return "删除成功";
                            if (value == 'scsb')
                                return "删除失败";
                            return "";
                        }}
                    ]
                ],
                onLoadSuccess:function(){
                    var rows = $('#grid_cwsf').datagrid("getRows");
                    if(rows.length>0){
                        $('#grid_cwsf').datagrid("selectRow", 0);
                    }
                    cwsf.setBtnStatus();
                    $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
                },
                onClickRow:function(){
                    cwsf.setBtnStatus();
                }
            });
        }
    });

    //**********按钮触发事件**********

    //查询按钮
    $("#btn_cwsf_search").click(function () {
        cwsf_search();
    });

    //撤销
    $("#btn_cwsf_cx").click(function () {
        cwsf_cx();
    });
    //重置
    $("#btn_cwsf_cz").click(function () {
        cwsf_cz();
    });
    
    //加载查询条件中的申报公司下拉框
    $("#cwsf_sbgs").combotree({
        loadFilter: function (rows) {
            return convert(rows);
        }
    });
//    //加载票据类型下拉框
//    $("#table_cwsf").find("[id='cwsf_pjlx_id']").sjzdcombobox("pjlx");

    //财务收付导入按钮
    $("#btn_cwsf_import").click(function () {
        cwsf_insert();
    });
});
 
//弹票据贴现窗口
function cwsf_insert() {
    var row = $('#grid_cwsf').datagrid('getSelected');
    if(row){
        var win = $.window({
            title:"财务收付导入",
            width:1000,
            height:300,
            cache:false,
            url:$.webPath() + "/cwgl/cwsf/cwsf_content?cwsfId="+row.id,
            modal:true,
            buttons:[
                {
                    text:'确认',
                    handler:function () {
                        $(win).mask();
                        $.ajax({
                            url:$.webPath() + "/cwgl/cwsf/generate_voucher",
                            dataType: 'json',
                            type: 'POST',
                            data:{"cwsfSystemDate":$("#cwsf_systemDate").datebox("getValue"),
                                "attachment":$("#cwsf_attachment").val(),
                                "bz":$("#cwsf_bz").val(),
                                "cwsfId":$("#cwsf_id").val()},
                            beforeSend: function () {
                                if($("#cwsf_attachment").val()=="" || $("#cwsf_attachment").val()==null){
                                    $.messager.alert("提示","请填写附单据数", "error");
                                    $(win).mask('hide');
                                    return false;
                                }
                                if($("#cwsf_systemDate").datebox("getValue")=="" || $("#cwsf_systemDate").datebox("getValue")==null){
                                    $.messager.alert("提示","请填写制单时间", "error");
                                    $(win).mask('hide');
                                    return false;
                                }
                            },
                            success:function(msg){
                                $(win).mask('hide');
//                                $("#layout_cwsf").mask("hide");
                                $(win).dialog('close');
                                if (msg.responseflag == 0){
                                    $('#grid_cwsf').datagrid('reload');
                                    $.messager.alert("提示", msg.responsemessage, "error");
                                } else {
                                    $('#grid_cwsf').datagrid('reload');
                                    $.messager.show({
                                        title: '提示',
                                        msg: msg.responsemessage,
                                        timeout: 3000,
                                        showType: 'slide'
                                    });
                                }
                            }
                        });
                    }
                }
            ]
        });
    }else{
        $.messager.alert('提示', '请选择一行数据', 'error');
    }
}
//财务收付撤销
function cwsf_cx(){
    var row = $('#grid_cwsf').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否撤销该财务收付？', function (r) {
            if (r) {
                var index = $('#grid_cwsf').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/cwgl/cwsf/revoke_voucher?cwsfId="+row.id,
                    dataType: 'json',
                    type: 'POST',
                    beforeSend: function () {
                        $("#layout_cwsf").mask();
                    },
                    success:function(msg){
                        $("#layout_cwsf").mask("hide");
                        if (msg.responseflag == 0){
                            $('#grid_cwsf').datagrid('reload');
                            $.messager.alert("提示", msg.responsemessage, "error");
                        }else {
                            $('#grid_cwsf').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: msg.responsemessage,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要撤销的收付款！", "warning");
    }
}



//贴现承兑重置
function cwsf_cz(){
    var row = $('#grid_cwsf').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否重置该收付款？', function (r) {
            if (r) {
                var index = $('#grid_cwsf').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/cwgl/cwsf/rest_voucher?cwsfId="+row.id,
                    dataType: 'json',
                    type: 'POST',
                    beforeSend: function () {
                        $("#layout_cwsf").mask();
                    },
                    success:function(msg){
                        $("#layout_cwsf").mask("hide");
                        if (msg.responseflag == 0){
                            $('#grid_cwsf').datagrid('reload');
                            $.messager.alert("提示", msg.responsemessage, "error");
                        }
                        else {
                            $('#grid_cwsf').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: msg.responsemessage,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要重置的财务收付！", "warning");
    }
}

//查询财务收付
function cwsf_search() {
    var cwsfZt = $('#cwsf_zt').combobox('getValue') == null ? null : $('#cwsf_zt').combobox('getValue');
    $('#grid_cwsf').datagrid('load',
        {
            cwsfpjlxMc:$("#cwsf_pjlx_id").val() || '',
            bm:$('#cwsf_bm').val(),
            cwsfdrZt:cwsfZt,
            sfkrqStart:$("#cwsf__sfkrq_start").datebox("getValue") || '',
            sfkrqEnd:$("#cwsf_sfkrq_end").datebox("getValue") || '',
          //  gldwMc:$('#cwsf_gldw').val(),
            gldwMc:$('#cwsf_gldw').attr("_text") == null ? null : $('#cwsf_gldw').attr("_text"),
            sbgsId:$('#cwsf_sbgs').combotree('getValue') == null ? null : $('#cwsf_sbgs').combotree('getValue'),
            sbgs:$('#cwsf_sbgs').combotree('getText') == null ? null : $('#cwsf_sbgs').combotree('getText')
        }
    );
}